<?php

namespace app\stores\model\cart;

use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\common\model\CommonModel;
use think\facade\Db;


class CartModel extends CommonModel{

    protected  $name = 'beautiful_cart';

    protected  $pk   = 'cart_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public function getCart($where)
    {
        $list = $this->where($where)->find();
        if($list) $list = $list->toArray();

        return $list;
    }

    public function saveCart($where,$save)
    {
        return $this->where($where)->save($save);
    }

    public function goods()

    {

        return $this->hasOne(GoodsModel::class,'goods_id','goods_id');

    }

    public function getCartList($member,$where,$order,$page=0,$limit=0,$field='*'){
        $query = $this->alias('c')
            ->join('beautiful_goods g','c.goods_id=g.goods_id')
            ->where(['g.is_delete'=>0])
            ->where($where);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->field($field)
            ->select()
            ->toArray();
        $total_num = 0;
        if(!empty($list)){
            $goodsSkuModel = new GoodsSkuModel();
            foreach ($list as $k=>$v){
                $list[$k]['goods'] = $v['goods'] = [
                    'goods_id'=>$v['goods_id'],
                    'name'=>$v['name'],
                    'send_type'=>$v['send_type'],
                    'code'=>$v['code'],
                    'cover_img'=>$v['cover_img'],
                    'original_price'=>$v['original_price'],
                    'take_count'=>$v['take_count'],
                    'quota'=>$v['quota'],
                    'fictitious_take_count'=>$v['fictitious_take_count'],
                    'vip_discount_type'=>$v['vip_discount_type'],
                    'is_vip_discount'=>$v['is_vip_discount'],
                    'discount_detail'=>$v['discount_detail'],
                    'status'=>$v['status'],
                    'crossed_price'=>$v['crossed_price'],
                    'start_sales_num'=>$v['start_sales_num'],
                    'origin_type'=>$v['origin_type'],
                    'is_by_sales'=>$v['is_by_sales']
                ];
                $sku = null;
                if($v['sku_id']){
                    $sku_where = [
                        'sku_id'=>$v['sku_id'],
                        'goods_id'=>$v['goods_id']
                    ];

                    $skus[0] = $sku = $goodsSkuModel->getById($sku_where,'sku_info,is_by_sales,name,goods_id,original_price,crossed_price,discount_detail,quota,start_sales_num');
                    
                    if($sku){
                        $skus[0]['origin_type'] = $v['origin_type'];
                        $return = skuMemberPrice($v['goods']['vip_discount_type'],$skus,$member,$v['shop_id'],$v['type']);
                        if($return){
                            $sku['price'] = sprintf("%.2f",$return[0]['price']);
                        }
                        unset($sku['discount_detail']);
                    }


                }
                $list[$k]['goods']['sku'] = $sku;
                $v['goods']['discount_detail'] =json_decode($v['goods']['discount_detail'],true);
                $return = getMemberPrice($member,$v['shop_id'],$v['goods'],$v['original_price'],$v['type']);
                $list[$k]['goods']['price'] = $return['price'];
                //配送
                $list[$k]['goods']['send_type'] = explode(',',$v['goods']['send_type']);
                unset($list[$k]['goods']['discount_detail']);

                $total_num+=$v['num'];
            }
        }
        return compact('count','list','total_num');
    }

    public static function getCashCartList($member,$where,$order,$page=0,$limit=0,$field='*'){
        $query =  Db::name('beautiful_cart')->alias('c')
            ->join('beautiful_goods g','c.goods_id=g.goods_id')
            ->where(['g.is_delete'=>0])
            ->where($where);
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->field($field)
            ->select()
            ->toArray();

        if(!empty($list)){
            $goodsSkuModel = new GoodsSkuModel();
            foreach ($list as $k=>$v){
                $sku = null;
                if($v['sku_id']){
                    $sku_where = [
                        'sku_id'=>$v['sku_id'],
                        'goods_id'=>$v['goods_id']
                    ];

                    $skus[0] = $sku = $goodsSkuModel->getById($sku_where,'name,goods_id,crossed_price,discount_detail,quota,start_sales_num');

                    if($sku){
                        $return = skuMemberPrice($v['goods']['vip_discount_type'],$skus,$member,$v['shop_id'],$v['type']);
                        if($return){
                            $sku['price'] = sprintf("%.2f",$return[0]['price']);
                        }
                        unset($sku['discount_detail']);
                    }


                }
                $list[$k]['goods']['sku'] = $sku;
                $v['goods']['discount_detail'] =json_decode($v['goods']['discount_detail'],true);
                $return = getMemberPrice($member,$v['shop_id'],$v['goods'],$v['original_price'],$v['type']);
                $list[$k]['goods']['price'] = $return['price'];
                //配送
                $list[$k]['goods']['send_type'] = explode(',',$v['goods']['send_type']);
                unset($list[$k]['goods']['discount_detail']);

                $total_num+=$v['num'];
            }
        }
        return compact('count','list','total_num');
    }

}

